package com.yowasa.xiuxian.engine.battle.strategy.drop.all;

import cn.hutool.core.util.RandomUtil;
import com.yowasa.xiuxian.annoations.BS;
import com.yowasa.xiuxian.engine.battle.strategy.BaseSpellStrategy;
import com.yowasa.xiuxian.entity.bo.battle.BattleContent;
import com.yowasa.xiuxian.entity.bo.battle.BuffContent;
import com.yowasa.xiuxian.entity.bo.battle.UserBattleContent;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.ElementEnum;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.BattleHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Map;

@BS(name = "五炁雷罚")
@Order(501)
@Component
public class WuQiLeiFa extends BaseSpellStrategy {
    @Autowired
    private BattleHelper battleHelper;

    @Override
    public ElementEnum getElement() {
        return null;
    }

    @Override
    public void action(UserBattleContent first, UserBattleContent second, BattleContent content) {
        Map<String, Integer> element = second.getUserBO().getElement();
        Collection<Integer> values = element.values();

        Long count = values.stream().filter(e -> RandomUtil.randomInt(1, 100) > 15 + e).count();
        double v =count*0.35;

        BuffContent leiHuo = second.getBuff().get("雷祸");
        if (leiHuo == null) {
            BuffContent buffContent = new BuffContent();
            buffContent.setDuration(6);
            buffContent.setNum(count.intValue());
            buffContent.setPositive(false);
            second.getBuff().put("雷祸", buffContent);
        }else {
            leiHuo.setDuration(6);
            leiHuo.setNum(leiHuo.getNum() + count.intValue());
        }

        int atk = first.getBattleInfo().getSpell();
        int def = second.getBattleInfo().getRes();
        int dmg = (int) (battleHelper.calSpell(atk, def) * v);

        int costMp = first.getBattleInfo().getMp() / 10;
        costMp = Math.max(costMp, 1000);
        first.getBattleInfo().setCmp(first.getBattleInfo().getCmp() - costMp);
        content.getMsgLog().add("五炁雷罚命中了"+count+"次");
        battleHelper.autoAction(this, first, second, content, dmg, "五炁雷罚");
        first.getCd().put("五炁雷罚", 5);
    }

    @Override
    public void limit(UserBO user) {
        if (user.getStage() < 5) {
            throw new HandlerException("学习/使用五炁雷罚阶位需要在如曜之金以上");
        }
    }

    @Override
    public String limitDesc() {
        return "学习/使用五炁雷罚阶位需要在如曜之金以上";
    }

    @Override
    public String desc() {
        return "释放5道雷霆，每道雷霆有0.35的倍率，每道雷霆会有15%+对方灵根浓度百分比的概率被闪避\ncd5回合，消耗最低1000点，最高10%的MP造成法术伤害\n每命中一道雷霆，为对方附上一层持续6回合的[雷祸]debuff，每层减少10%法抗(最高减少50%)";
    }

    @Override
    public boolean check(UserBattleContent user, BattleContent content) {
        Integer cd = user.getCd().get("五炁雷罚");
        if (cd != null && cd > 0) {
            return false;
        }
        int costMp = user.getBattleInfo().getMp() / 10;
        costMp = Math.max(costMp, 1000);
        if (user.getBattleInfo().getCmp() < costMp) {
            return false;
        }
        return true;
    }
}
